#!/usr/bin/env python
# -*- encoding: utf-8 -*-
'''
@File    :   homework.py
@Time    :   2020/06/28 23:25:48
@Author  :   Qing Bei
@Version :   1.0
@Contact :   1013575196@qq.com
@License :   (C)Copyright 2017-2018, Liugroup-NLPR-CASIA
@Desc    :   IP Alive Check
'''
# here put the import lib
import IPy

def Ping_IP(ip)->'BOOL':

    import subprocess

    # ip_str = IPy.IP(ip)
    istr_1 = str( IPy.IP(ip).net() )

    p = subprocess.call("ping -n 1 %s"%istr_1, shell=True, stdout=open(r'ping.temp', 'w'), stderr=subprocess.STDOUT)

    if p == 0:

        return True

    else:

        return False

def legalIP(ip):

    try:

        IPy.IP(ip)

        return True

    except Exception as e:

        return False

def main():

     theStr = input('Please input the IP you want to check,for example: 192.168.0.0/24:')

     if legalIP(theStr):

        if Ping_IP(theStr):

            print(f'The IP:{theStr} is alive')

        else:

            print(f'The IP:{theStr} is down')

     else:

        print('Ilegal IP!')

if __name__ == "__main__":

    main()